package com.allwees.bs.c.module.order.controller;

import com.allwees.bs.c.module.base.controller.BaseController;
import com.allwees.bs.c.module.order.dto.CartDTO;
import com.allwees.bs.c.module.order.req.CartCheckoutReq;
import com.allwees.bs.c.module.order.req.CartReq;
import com.allwees.bs.c.module.order.req.CartUseCashReq;
import com.allwees.bs.c.module.order.service.CartService;
import com.allwees.bs.c.module.security.AnonymousAccess;
import com.allwees.bs.core.model.user.context.CurrentUser;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.core.common.util.MoneyUtil;
import com.allwees.core.common.util.http.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * 购物车相关
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/cart")
@Validated
public class CartController extends BaseController {

    @Autowired
    private CartService cartService;

    /**
     * 获取当前用户购物车
     *
     * @param cartUuid
     * @return CartDTO
     */
    @GetMapping("/{cartUuid}")
    public R getCart(@PathVariable("cartUuid") String cartUuid) {
        CartDTO cartDTO = this.cartService.getCart(cartUuid);
        calculateShowCashBalance(cartDTO);
        return R.ok(cartDTO);
    }

    private void calculateShowCashBalance(CartDTO cart) {
        cart.setShowCashBalance(MoneyUtil.sub(cart.getUserAmtCash(), cart.getAmtCash()));
    }

    /**
     * 增加或减少指定数量SKU到购物车
     *
     * @param req
     * @return
     */
    @AnonymousAccess
    @PostMapping("/add-or-reduce")
    public R addOrReduce(@RequestBody CartReq req) {
        CartDTO cart = null;
        if (req.isNotLogined()) {
            cart = cartService.calculateCartItems(req.getCartItems());
        } else {
            String userUuid = CurrentUser.getUser().getUserUuid();
            if (req.getCount() > 0) {
                cart = cartService.addSku(req.getCartUuid(), req.getSkuUuid(), req.getCount(), userUuid, RequestContext.getChannel());
            } else if (req.getCount() < 0) {
                cart = cartService.reduceSku(req.getCartUuid(), req.getSkuUuid(), Math.abs(req.getCount()), RequestContext.getChannel());
            }
        }
        log.info("====add-or-reduce接口返回:{}", cart);
        calculateShowCashBalance(cart);
        return R.ok(cart);
    }

    /**
     * 删除购物车的指定SKU项
     *
     * @param req
     * @return
     */
    @AnonymousAccess
    @PostMapping("/delete")
    public R deleteCartItem(@RequestBody CartReq req) {
        CartDTO cart;
        if (req.isNotLogined()) {
            cart = cartService.calculateCartItems(req.getCartItems());
        } else {
            cart = cartService.deleteCartItem(req);
        }
        calculateShowCashBalance(cart);
        return R.ok(cart);
    }

    /**
     * 清空购物车
     *
     * @param req
     * @return
     */
    @RequestMapping("/clear")
    public R clearCart(@RequestBody CartReq req) {
        CurrentUser.getUser();
        this.cartService.clearCart(req.getCartUuid());
        return R.ok();
    }


    /**
     * 登录时把未登录时加入的购物车合并到登录后购物车中
     *
     * @param req
     * @return CartDTO
     */
    @PostMapping("/merge-items")
    public R mergeCartItems(@RequestBody CartReq req) {
        String userUuid = CurrentUser.getUser().getUserUuid();
        String channel = RequestContext.getChannel();
        return R.ok(cartService.mergeCartItems(req.getCartUuid(), req.getCartItems(), userUuid, channel));
    }

    /**
     * choose coupon
     *
     * @return CartDTOs
     */
    @PostMapping("/{cartUuid}/use-coupon")
    public R chooseCoupon(@PathVariable("cartUuid") String cartUuid, @RequestBody CartCheckoutReq req) {
        log.info("====use-coupon接口参数:{}", req);
        CartDTO dto = this.cartService.useCoupon(CurrentUser.getUser().getUserUuid(), req);
        log.info("====use-coupon接口返回:{}", dto);
        calculateShowCashBalance(dto);
        return R.ok(dto);
    }

    /**
     * choose cash
     *
     * @return CartDTOs
     */
    @PostMapping("/{cartUuid}/use-cash")
    public R chooseCash(@PathVariable("cartUuid") String cartUuid, @RequestBody CartUseCashReq req) {
        log.info("====use-cash接口参数:{}", req);
        req.setCartUuid(cartUuid);
        CartDTO dto = this.cartService.useCash(CurrentUser.getUser().getUserUuid(), req);
        log.info("====use-cash接口返回:{}", dto);
        calculateShowCashBalance(dto);
        return R.ok(dto);
    }
}
